/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editor.mouseoveraction;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.Locator;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Handle;
import org.eclipse.gef.handles.AbstractHandle;
import org.eclipse.gef.palette.PaletteEntry;


/**
 * 
 */
public class ActionHandleManager 
{
	
	/**
	 * 
	 */
	public static final Object ACTION_HANDLE_EDIT_POLICY = "ActionHandleManager.ACTION_HANDLE_EDIT_POLICY";
	
	/**
	 * 
	 */
	protected GraphicalEditPart ownerEditPart;
	
	/**
	 * 
	 */
	protected List<Handle> handles;
	
	/**
	 * 
	 */
	protected ActionHandleFactory handleFactory;
   
   /**
    * 
    * 
    * @return 
    */
   public ActionHandleFactory getHandleFactory() {
	   if(handleFactory==null) {
		handleFactory = createHandleFactory();
	}
		return handleFactory;
	}

   /**
    * Create the associated HandleFactory.
    * Subclasses may overide this method.
    * 
    * @return 
    */
	private ActionHandleFactory createHandleFactory() {
	return new ActionHandleFactory();
	
}

	/**
	 * 
	 * 
	 * @param factory 
	 */
	public void setHandleFactory(ActionHandleFactory factory) {
		this.handleFactory = factory;
	}

/**
 * 
 * 
 * @param editPart 
 */
   public ActionHandleManager(GraphicalEditPart editPart) 
   {
    this.ownerEditPart = editPart;
    handles = new ArrayList<Handle>();
    init();
   }
   
    
   /**
    * Add the tool as a mouse over handle.
    * Create the corresponding ActionHandle.
    * Create its locator. Add it to the Handle.
    * Add the handle to the list.
    * 
    * @param tool     * @param toolEntry 
    * @param location use PositionConstants
    */
   public void addTool(PaletteEntry toolEntry, int location) 
   {
	   addTool( toolEntry, location, true, 3);
   }
   
   
   /**
    * Add a tool at the specified location, using the specified offset.
    * 
    * @param isInside 
    * @param location 
    * @param toolEntry 
    * @param offset 
    */
   public void addTool(PaletteEntry toolEntry, int location, boolean isInside, int offset) 
   {
	   
	   Locator locator = null;
	   if( isInside) {
		locator = new ActionHandleInsideLocator(ownerEditPart.getFigure(), location);
	} else {
		locator = new ActionHandleOutsideLocator(ownerEditPart.getFigure(), location);
	}

	   AbstractHandle handle = getHandleFactory().createActionHandleFor(ownerEditPart, toolEntry, locator);
	   handles.add( handle );
   }

   /**
    * Add the tool as a mouse over handle.
    * Create the corresponding ActionHandle.
    * Create its locator. Add it to the Handle.
    * Add the handle to the list.
    * @param tool
    * @param location use PositionConstants
    */
//   public void addTool(CreationToolEntry toolEntry, int location) 
//   {
//      handles.add( new CreationToolEntryActionHandle( ownerEditPart, toolEntry, new ActionHandleOutsideLocator( ownerEditPart.getFigure(), location) ) );
//   }
   
   
   /**
    * Initialize Manager.
    * Add the editPolicy to the EditPart.
    */
   public void init() 
   {
    ownerEditPart.installEditPolicy(ACTION_HANDLE_EDIT_POLICY, new ActionHandleEditPolicy(this) );
   }

   /**
    * Get the list of handles.
    * 
    * @return 
    */
	public List getHandles() {
		// TODO Auto-generated method stub
		return handles;
	}
}
